#include "cguidefaultstylesloader.h"

/**
 * loadStyle - reads style of components from APF file
 * @param fileName
 * @param logPtr
 * @return default styles
 */
CDefaultStyles CGuiDefaultStylesLoader::loadStyle(string fileName,
    CLogger* logPtr) {
  CAPFNode data;
  CAPFParser::parseAPF(fileName, data, logPtr);
  return CDefaultStyles(readRootNode(&data));
}
//---------------------------------------------------------------------------

/**
 * readRootNode - reads root APF node
 * @param node
 * @return map with all default styles
 */
map<int, map<int, CGuiStyle> > CGuiDefaultStylesLoader::readRootNode(
    CAPFNode* node) {

  // Result map
  map<int, map<int, CGuiStyle> > result;

  // Child nodes
  vector<CAPFNode*>::const_iterator i = node->getSubNodesPtr()->begin();
  for (; i < node->getSubNodesPtr()->end(); i++) {
    if ((*i)->getName().compare("DEFAULT_DIALOG") == 0) {
      result.insert(make_pair(DEFAULT_DIALOG, readDialog(i)));
    } else if ((*i)->getName().compare("DEFAULT_BUTTON") == 0) {
      result.insert(make_pair(DEFAULT_BUTTON, readComponent(i)));
    } else if ((*i)->getName().compare("DEFAULT_COMBO") == 0) {
      result.insert(make_pair(DEFAULT_COMBO, readComponent(i)));
    } else if ((*i)->getName().compare("DEFAULT_EDIT") == 0) {
      result.insert(make_pair(DEFAULT_EDIT, readComponent(i)));
    } else if ((*i)->getName().compare("DEFAULT_LABEL") == 0) {
      result.insert(make_pair(DEFAULT_LABEL, readComponent(i)));
    } else if ((*i)->getName().compare("DEFAULT_SPIN") == 0) {
      result.insert(make_pair(DEFAULT_SPIN, readComponent(i)));
    }
  }
  return result;
}
//---------------------------------------------------------------------------

/**
 * readDialog - reads style of dialog
 * @param node
 * @return map with dialog style
 */
map<int, CGuiStyle> CGuiDefaultStylesLoader::readDialog(
    vector<CAPFNode*>::const_iterator node) {
  map<int, CGuiStyle> result;
  vector<CAPFNode*>::const_iterator i = (*node)->getSubNodesPtr()->begin();
  for (; i < (*node)->getSubNodesPtr()->end(); i++) {
    if ((*i)->getName().compare("STYLE_DEFAULT") == 0) {
      result.insert(make_pair(STYLE_DEFAULT, readStyle(i)));
    }
  }
  return result;
}
//---------------------------------------------------------------------------

/**
 * readComponent - reads styles of component
 * @param node
 * @return map with component styles
 */
map<int, CGuiStyle> CGuiDefaultStylesLoader::readComponent(
    vector<CAPFNode*>::const_iterator node) {
  map<int, CGuiStyle> result;
  vector<CAPFNode*>::const_iterator i = (*node)->getSubNodesPtr()->begin();
  for (; i < (*node)->getSubNodesPtr()->end(); i++) {
    if ((*i)->getName().compare("STYLE_NORMAL") == 0) {
      result.insert(make_pair(STYLE_NORMAL, readStyle(i)));
    } else if ((*i)->getName().compare("STYLE_PRELIGHT") == 0) {
      result.insert(make_pair(STYLE_PRELIGHT, readStyle(i)));
    } else if ((*i)->getName().compare("STYLE_FOCUS") == 0) {
      result.insert(make_pair(STYLE_FOCUS, readStyle(i)));
    } else if ((*i)->getName().compare("STYLE_DISABLED") == 0) {
      result.insert(make_pair(STYLE_DISABLED, readStyle(i)));
    }
  }
  return result;
}
//---------------------------------------------------------------------------

/**
 * readStyle - reads style
 * @param node
 * @return style
 */
CGuiStyle CGuiDefaultStylesLoader::readStyle(
    vector<CAPFNode*>::const_iterator node) {
  CGuiStyle style;
  vector<CAPFNode*>::const_iterator i = (*node)->getSubNodesPtr()->begin();
  for (; i < (*node)->getSubNodesPtr()->end(); i++) {
    if ((*i)->getName().compare("color") == 0) {
      style.setColor(CColor(CMath<int>::parseHex((*i)->getValue())));
    } else if ((*i)->getName().compare("alpha") == 0) {
      style.setAlpha(CMath<unsigned char>::parse((*i)->getValue()));
    } else if ((*i)->getName().compare("imgId") == 0) {
      style.setImgId(CMath<int>::parse((*i)->getValue()));
    } else if ((*i)->getName().compare("border") == 0) {
      style.setBorder(readBorder(i));
    }
  }
  return style;
}
//---------------------------------------------------------------------------

/**
 * readBorder - reads style of border
 * @param node
 * @return border style
 */
CBorder CGuiDefaultStylesLoader::readBorder(
    vector<CAPFNode*>::const_iterator node) {
  CBorder border;
  vector<CAPFNode*>::const_iterator i = (*node)->getSubNodesPtr()->begin();
  for (; i < (*node)->getSubNodesPtr()->end(); i++) {
    if ((*i)->getName().compare("color") == 0) {
      border.setColor(CColor(CMath<int>::parseHex((*i)->getValue())));
    } else if ((*i)->getName().compare("alpha") == 0) {
      border.setAlpha(CMath<unsigned char>::parse((*i)->getValue()));
    } else if ((*i)->getName().compare("size") == 0) {
      border.setSize(CMath<int>::parse((*i)->getValue()));
    }
  }
  return border;
}
//---------------------------------------------------------------------------
